package com.hoyo.common;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class PageInfo<E> {
	
	private int pageNum;
	
	private int pageSize;
	
	private int pages;
	
	private int total;
	
	private int navigatePages;
	
	private int size;
	
	private int[] navigatePageNums;
	
	private boolean isFirstPage;
	
	private boolean isLastPage;
	
	private boolean hasPreviousPage;
	
	private boolean hasNextPage;
	
	private List<E> list;

	public PageInfo() {
	}
	
	public PageInfo(int pageNum, int pageSize, int total, List<E> list) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.total = total;
		this.list = list;
		this.size = list.size();
		init();
	}

	private void init() {
		this.pages = this.total%this.pageSize==0?this.total/this.pageSize:this.total/this.pageSize+1;
		this.navigatePages = 8;
		int[] navigatePageNums;
		if(this.pages<=this.navigatePages){
			navigatePageNums= new int[this.pages];
			for(int i=0;i<navigatePageNums.length; i++){
				navigatePageNums[i] = i+1;
			}
		}else{
			navigatePageNums= new int[this.navigatePages];
			if(this.pageNum-this.navigatePages/2<=0){
				for(int i=0;i<navigatePageNums.length; i++){
					navigatePageNums[i] = i+1;
				}
			}else if(this.pageNum+this.navigatePages/2>this.pages){
				for(int i=0;i<navigatePageNums.length; i++){
					navigatePageNums[i] = this.pages-this.navigatePages+i+1;
				}
			}else{
				for(int i=0;i<navigatePageNums.length; i++){
					navigatePageNums[i] = this.pageNum-this.navigatePages/2+i;
				}
			}
		}
		this.navigatePageNums = navigatePageNums;
		this.isFirstPage = pageNum == 1;
		this.isLastPage = pageNum == pages;
		this.hasPreviousPage = pageNum>1;
		this.hasNextPage = pageNum<pages;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPages() {
		return pages;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getNavigatePages() {
		return navigatePages;
	}

	public int[] getNavigatePageNums() {
		return navigatePageNums;
	}

	public int getSize() {
		return size;
	}

	public boolean getIsFirstPage() {
		return isFirstPage;
	}

	public boolean getIsLastPage() {
		return isLastPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public List<E> getList() {
		return list;
	}

	public void setList(List<E> list) {
		this.list = list;
	}
	
	public static void main(String[] args) {
		System.out.println(JSONObject.toJSONString(new PageInfo<>(11, 3, 31, new ArrayList<>())));
	}
}
